package com.kld.common.webServiceUtil;

import cn.hutool.json.JSONObject;
import com.kld.common.webServiceUtil.weixin.JsonObject;
import com.kld.common.webServiceUtil.weixin.weixinClient;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

public class weixinClientUtil {

    //获取access_token
    public static JsonObject getAccessToken(){
        String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wwde3a892bb0fb3d76&corpsecret=ZMLGrds7_xwn8HA6usDSzaMy8kYcaZvwmilwACKZgyk";
        weixinClient weixinClient = new weixinClient();
        ResponseEntity<JsonObject> responseEntity = weixinClient.exchange(url,HttpMethod.GET,null,JsonObject.class);
        JsonObject result = responseEntity.getBody();
        if(result.getErrcode() != 0 ){
            throw new RuntimeException(result.getErrmsg());
        }
        return result;
    }

    //获取企业微信用户名
    //人员编码列表，多个以‘|’区分，最大不超过1000
    public static JsonObject getEnterpriseWeChatUser(String userEncode){
        String url = "https://vpn.sxycpc.com/portal/getEnterpriseWeChatUser";
        weixinClient weixinClient = new weixinClient();
        MultiValueMap<String,Object> data = new LinkedMultiValueMap<>();
        data.add("userEncode",userEncode);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<Map<String,Object>> httpEntity = new HttpEntity(data,httpHeaders);
        ResponseEntity<JsonObject> responseEntity = weixinClient.exchange(url,HttpMethod.POST,httpEntity,JsonObject.class);
        JsonObject result = responseEntity.getBody();
        if(result.getStatus() != 200 ){
            throw new RuntimeException(result.getErrmsg());
        }
        return result;
    }

    public static JsonObject sendMessage(String touser,String message,String access_token){
        String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+access_token;
        weixinClient weixinClient = new weixinClient();
        //MultiValueMap<String,Object> data = new LinkedMultiValueMap<>();
        JSONObject data = new JSONObject();
        data.set("touser",touser);
        data.set("toparty",null);
        data.set("msgtype","text");
        data.set("agentid","1000044");
        //MultiValueMap<String,Object> msg = new LinkedMultiValueMap<>();
        JSONObject msg = new JSONObject();
        msg.set("content",message);
        data.set("text",msg);
        data.set("safe",0);
        data.set("enable_id_trans",0);
        data.set("enable_duplicate_check",0);
        data.set("duplicate_check_interval",0);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String,Object>> httpEntity = new HttpEntity(data,httpHeaders);
        ResponseEntity<JsonObject> responseEntity = weixinClient.exchange(url,HttpMethod.POST,httpEntity,JsonObject.class);
        JsonObject result = responseEntity.getBody();
        if(result.getErrcode() != 0 ){
            throw new RuntimeException(result.getErrmsg());
        }
        return result;
    }

    public static void main(String[] args) {
        //System.out.println(getAccessToken().getAccess_token());
        //System.out.println(getEnterpriseWeChatUser("11102973").toString());
        System.out.println(sendMessage("caonan","ceshiceshi","YzFFB5mNHcK_hMOCQ9zg8at-ujAcdbMUDU-G7hzGl39u9CBZrX722GFL5luAsy_4XPMMPjLEB7qScW9RxMYnMIM3JcJNypnB2SKPVt7_qAiZXgQo4LFP9CgpjCPdJP2279fRsKmq1utwdERNRDv2tpQFzemVmRpj6qx9gEK8BMT7s9x8ULByLi0FwsMLL7I0o3a7hJ5rKTx7nUHjsXowZA"));
    }
}
